#pragma once
#include<vector>
#include<functional>

namespace cx
{

	template<class T>
	class myless
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<class T>
	class mygreater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T,class Container = vector<T>,class Compare = myless<T>>
	class priority_queue
	{
	public:
		
		priority_queue() = default;

		template<class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_con.push_back(*first);
				++first;
			}

			for (int i = (size() - 1 - 1) / 2; i >= 0; i--)
			{
				AdjushDown(i);
			}
		}


		void AdjustUp(int child)
		{
			Compare comfunc;
			int parent = (child - 1) / 2;

			while(child > 0)
			{
				if (comfunc(_con[parent] , _con[child]))
				{
					swap(_con[parent], _con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& val)
		{
			_con.push_back(val);
			AdjustUp(size() - 1);
		}

		void AdjushDown(int parent)
		{
			Compare comfunc;
			size_t child = parent * 2 + 1;

			if (child+1 < size() && comfunc(_con[child], _con[child+1]))
			{
				child = child + 1;
			}

			while (child < size())
			{
				if (comfunc(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}

		}

		void pop()
		{
			swap(_con[0], _con[size() - 1]);
			_con.pop_back();
			AdjushDown(0);
		}

		T& top()
		{
			return _con[0];
		}

		const T& top() const
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}

	private:
		Container _con;
	};
}